﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Backend.JYD.BLL.Bid;
using Backend.JYD.Model.Bid;
using Backend.JYD.Common;
using Backend.JYD.RunTime;

namespace Backend.JYD.Web.Controllers
{
    public class BidRecordController : BaseController
    {
        //
        // GET: /Invite/
        public InviteInfo m_invite;//需获取相关的标的对象
        public BidStat m_stat;//需获取相关标的统计对象

        BidRecordBLL bll = new BidRecordBLL();
        public ActionResult Index()
        {
            //增加当前员工条件
            var list = bll.GetdBidRecordsByInviteId(1);
            
            return View(list);
        }

        //
        // GET: /Invite/Details/5

        public ActionResult Details(Int64 bidid)
        {
            var info = bll.GetdBidRecordByBidId(bidid);
            return View(info);
        }

        //
        // GET: /Invite/Create

        public ActionResult Create(Int64 inviteid)
        {
            BidRecord record = new BidRecord();
            record.InviteId = inviteid;
            return View(record);
        } 

        //
        // POST: /Invite/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                BidRecord record = new BidRecord();
                record.InviteId = NumberHelper.StrToInt64(collection["InviteId"]);
                record.CreateTime = DateTime.Now;
                record.LoanMoney = NumberHelper.StrToDecimal(collection["LoanMoney"]);
                record.Rate = NumberHelper.StrToDecimal(collection["Rate"]);
                record.OpId = this.EmpId;
                record.Status = 1;
                record.Term = NumberHelper.StrToInt(collection["Term"]);
                record.Type = 1;
                record.UpdateTime = DateTime.Now;
                record.BidId = bll.BidInvite(m_invite, record, m_stat);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Invite/Edit/5

        public ActionResult Edit(Int64 bidid)
        {
            var info = bll.GetdBidRecordByBidId(bidid);
            return View(info);
        }

        //
        // POST: /Invite/Edit/5

        [HttpPost]
        public ActionResult Edit(Int64 bidid, FormCollection collection)
        {
            try
            {
                BidRecord old = new BidRecord();//需获取原投标对象

                // TODO: Add update logic here
                BidRecord record = new BidRecord();
                record.CreateTime = DateTime.Now;
                record.LoanMoney = NumberHelper.StrToDecimal(collection["LoanMoney"]);
                record.Rate = NumberHelper.StrToDecimal(collection["Rate"]);
                record.OpId = this.EmpId;
                record.Status = 1;
                record.Term = NumberHelper.StrToInt(collection["Term"]);
                record.Type = 1;
                record.UpdateTime = DateTime.Now;

                if (record.BidId > 0)
                    bll.UpdateBidding(m_invite,old,record,m_stat);
                else
                    record.BidId = bll.BidInvite(m_invite, record,m_stat);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
